<?php


namespace App\Utils\Wechat;


use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Guzzle\CoroutineHandler;
use Hyperf\Guzzle\HandlerStackFactory;
use Hyperf\HttpServer\Request;
use Hyperf\Redis\Redis;
use Overtrue\Socialite\Providers\AbstractProvider;
use Psr\SimpleCache\CacheInterface;

class Base
{
    public $request;

    public $config;

    public $app;

    /**
     * @Inject()
     * @var Redis
     */
    public $redis;

    public function __construct()
    {
        $this->request = di(Request::class);

        $config = $this->app['config']->get('http', []);
        $config['handler'] = di(HandlerStackFactory::class)->create();
        $this->app->rebind('http_client', new Client($config));

        // 重写 Handler
        $this->app['guzzle_handler'] = new CoroutineHandler();

        // 设置 OAuth 授权的 Guzzle 配置
        AbstractProvider::setGuzzleOptions([
            'http_errors' => false,
            'handler' => HandlerStack::create(new CoroutineHandler()),
        ]);

        // 替换缓存
        $this->app['cache'] = di(CacheInterface::class);
    }
}